package com.logichina.csms.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.logichina.csms.bean.DealerBean;
import com.logichina.csms.bean.ResultBean;
import com.logichina.csms.dao.IDealerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Service
public class DealerService implements IDealerService {

    @Autowired
    private IDealerRepository iDealerRepository;


    @Override
    public String saveDealer(String dealer) {
        JSONObject fastJson = JSON.parseObject(dealer);
        JSONArray detailArray = fastJson.getJSONArray("body");
        //返回信息类
        ResultBean rb = new ResultBean();
        if (detailArray != null)
        {
            for (Iterator<Object> iterator = detailArray.iterator(); iterator.hasNext(); )
            {
                //创建对象
                DealerBean dealerBean = new DealerBean();
                //创建必填字段容器
                Map<String,String> map = new HashMap<>();
                //解析json数组字符串
                JSONObject json = (JSONObject) iterator.next();
                String dealerName = json.getString("DEALERNAME");
                map.put("DEALERNAME",dealerName);
                String dealerCode = json.getString("DEALERCODE");
                map.put("DEALERCODE",dealerCode);
                Date date = json.getDate("DATE");
                String orderCode = json.getString("ORDERCODE");
                map.put("ORDERCODE",orderCode);
                String orderPersonId = json.getString("ORDERPERSONID");
                map.put("ORDERPERSONID",orderPersonId);
                String orderPersonCode = json.getString("ORDERPERSONCODE");
                map.put("ORDERPERSONCODE",orderPersonCode);
                String orderPersonName = json.getString("ORDERPERSONNAME");
                map.put("ORDERPERSONNAME",orderPersonName);
                String contact = json.getString("CONTACT");
                map.put("CONTACT",contact);
                String fixPhoneAreaCode = json.getString("FIXPHONE_AREACODE");
                String fixPhone = json.getString("FIXPHONE");
                String mobile = json.getString("MOBILE");
                String brand = json.getString("BRAND");
                map.put("BRAND",brand);
                String clientType = json.getString("CLIENTTYPE");
                map.put("CLIENTTYPE",clientType);
                String clientName = json.getString("CLIENTNAME");
                map.put("CLIENTNAME",clientName);
                String salePersonName = json.getString("SALEPERSONNAME");
                map.put("SALEPERSONNAME",salePersonName);
                String saleContact = json.getString("SALECONTACT");
                map.put("SALECONTACT",saleContact);
                String certificateType = json.getString("CERTIFICATETYPE");
                map.put("CERTIFICATETYPE",certificateType);
                String certificateNumber = json.getString("CERTIFICATENUMBER");
                map.put("CERTIFICATENUMBER",certificateNumber);
                String inProvice = json.getString("IN_PROVINCE");
                map.put("IN_PROVINCE",inProvice);
                String inProviceId = json.getString("IN_PROVINCEID");
                map.put("IN_PROVINCEID",inProviceId);
                String inCity = json.getString("IN_CITY");
                map.put("IN_CITY",inCity);
                String inCityId = json.getString("IN_CITYID");
                map.put("IN_CITYID",inCityId);
                String inCounty = json.getString("IN_COUNTY");
                map.put("IN_COUNTY",inCounty);
                String inCountyId = json.getString("IN_COUNTYID");
                map.put("IN_COUNTYID",inCountyId);
                String inAddress = json.getString("IN_ADDRESS");
                map.put("IN_ADDRESS",inAddress);
                String addressRange = json.getString("ADDRESSRANGE");
                map.put("ADDRESSRANGE",addressRange);
                String model = json.getString("MODEL");
                map.put("MODEL",model);
                String vin = json.getString("VIN");
                map.put("VIN",vin);
                int credit;
                int deposit;
                int indoor;
                int outdoor;
                //字段类型检测
                try{
                    credit = json.getInteger("CREDIT");
                    deposit = json.getInteger("DEPOSIT");
                    indoor = json.getInteger("INDOOR");
                    outdoor = json.getInteger("OUTDOOR");
                }catch (Exception e){
                    rb.setCode(103);
                    rb.setMessage("字段类型错误，填写字段不为整数型:" + e.getMessage());
                    return JSON.toJSONString(rb);
                }
                //必填字段为空检测
                for(Map.Entry<String, String> entry : map.entrySet()){
                    String mapKey = entry.getKey();
                    String mapValue = entry.getValue();
                    if("".equals(mapValue)||mapValue==null){
                        rb.setCode(101);
                        rb.setMessage("必填字段："+ mapKey + " 为空错误：");
                        return JSON.toJSONString(rb);
                    }
                }
                //字段内容规范检测
                if(!clientType.equals("个人")&&!clientType.equals("公司")){
                    rb.setCode(102);
                    rb.setMessage("字段CLIENTTYPE："+ clientType + " 填写范围：个人、公司");
                    return JSON.toJSONString(rb);
                }
                if(!certificateType.equals("身份证")&&!certificateType.equals("护照")&&!certificateType.equals("通行证")
                        &&!certificateType.equals("统一社会信用代码")){
                    rb.setCode(102);
                    rb.setMessage("字段CERTIFICATETYPE："+ certificateType + " 填写范围：身份证、护照、" +
                            "通行证、统一社会信用代码");
                    return JSON.toJSONString(rb);
                }
                if(credit!=0&&credit!=1&deposit!=0&&deposit!=1){
                    rb.setCode(102);
                    rb.setMessage("字段CREDIT 和 DEPOSIT 填写范围：0：未过  1：已过");
                    return JSON.toJSONString(rb);
                }
                if(indoor!=1&&outdoor!=1&&indoor!=0&&outdoor!=0){
                    rb.setCode(102);
                    rb.setMessage("字段INDOOR 和 OUTDOOR 填写内容只能为 1：ture 0：false");
                    return JSON.toJSONString(rb);
                }

                //对象赋值
                dealerBean.setDealerName(dealerName);
                dealerBean.setDealerCode(dealerCode);
                dealerBean.setDate(date);
                dealerBean.setOrderCode(orderCode);
                dealerBean.setOrderPersonId(orderPersonId);
                dealerBean.setOrderPersonCode(orderPersonCode);
                dealerBean.setOrderPersonName(orderPersonName);
                dealerBean.setContact(contact);
                dealerBean.setFixphoneAreaCode(fixPhoneAreaCode);
                dealerBean.setFixphone(fixPhone);
                dealerBean.setMobile(mobile);
                dealerBean.setBrand(brand);
                dealerBean.setClientType(clientType);
                dealerBean.setClientName(clientName);
                dealerBean.setSalePersonName(salePersonName);
                dealerBean.setSaleContact(saleContact);
                dealerBean.setCertificateType(certificateType);
                dealerBean.setCertificateNumber(certificateNumber);
                dealerBean.setInProvince(inProvice);
                dealerBean.setInProvinceId(inProviceId);
                dealerBean.setInCity(inCity);
                dealerBean.setInCityId(inCityId);
                dealerBean.setInCounty(inCounty);
                dealerBean.setInCountyId(inCountyId);
                dealerBean.setInAddress(inAddress);
                dealerBean.setAddressRange(addressRange);
                dealerBean.setModel(model);
                dealerBean.setCredit(credit);
                dealerBean.setDeposit(deposit);
                dealerBean.setVin(vin);
                dealerBean.setIndoor(indoor);
                dealerBean.setOutdoor(outdoor);
                //存入缓存
                iDealerRepository.save(dealerBean);
            }
        }
        //存入数据库
        iDealerRepository.flush();
        rb.setCode(200);
        rb.setMessage("数据传输成功");
        return JSON.toJSONString(rb);
    }
}
